package com.labichaoka.chaoka.utils;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.MyApplication;

public class ToastUtils extends Toast {

    /**
     * Toast颜色_黑色
     */
    public static final int COLOR_BLACK = 1;
    /**
     * Toast颜色_黄色
     */
    public static final int COLOR_MAIN = 2;

    /**
     * Toast单例
     */
    private static ToastUtils toast;

    /**
     * Toast容器
     */
    private static LinearLayout toast_layout;

    /**
     * 构造
     *
     * @param context
     */
    private static Context mAppContext = MyApplication.mContext;

    /**
     * 显示Toast
     *
     * @param text    显示的文本
     */
    public static void show(CharSequence text) {
        show(mAppContext, text, COLOR_MAIN);
    }

    public ToastUtils(){
        super(mAppContext);
    }

    /**
     * 显示Toast
     *
     * @param context   上下文
     * @param text      显示的文本
     * @param colorType 背景颜色
     */
    public static void show(Context context, CharSequence text, int colorType) {
        if (text == null || "".equals(text)){
            return;
        }

        // 初始化一个新的Toast对象
        initToast(context, text, colorType);

        toast.setDuration(text.length() < 15 ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG);

        toast_layout.setPivotY(0);
        toast_layout.setScaleY(0f);
        ObjectAnimator anim = ObjectAnimator.ofFloat(toast_layout, "scaleY", 0, 1);
        anim.setStartDelay(200);
        anim.setDuration(800);
        anim.setInterpolator(new BounceInterpolator());
        anim.start();

        // 显示Toast
        toast.show();

    }

    /**
     * 初始化Toast
     *
     * @param context   上下文
     * @param text      显示的文本
     * @param colorType 背景颜色
     */
    private static void initToast(Context context, CharSequence text, int colorType) {
        try {
            cancelToast();

            toast=new ToastUtils();
            // 获取LayoutInflater对象
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 由layout文件创建一个View对象
            View layout = inflater.inflate(R.layout.toast_layout, null);

            // 吐司容器
            toast_layout = (LinearLayout) layout.findViewById(R.id.toast_layout);

            changeColor(colorType);

            // 实例化ImageView和TextView对象
            TextView toast_text = (TextView) layout.findViewById(R.id.toast_text);
            toast_text.setText(text);
            toast.setView(layout);
            toast.setGravity(Gravity.BOTTOM, 0, 200);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 改变背景颜色
     *
     * @param colorType 背景颜色
     */
    private static void changeColor(int colorType) {
        switch (colorType) {
            case COLOR_MAIN:
                toast_layout.setBackgroundResource(R.drawable.toast_main);
                break;
            case COLOR_BLACK:
                toast_layout.setBackgroundResource(R.drawable.toast_black);
                break;
            default:
                toast_layout.setBackgroundColor(colorType);
                break;
        }
    }

    /**
     * 隐藏当前Toast
     */
    public static void cancelToast() {
        if (toast != null) {
            toast.cancel();
        }
    }

    public void cancel() {
        try {
            super.cancel();
        } catch (Exception e) {

        }
    }

    @Override
    public void show() {
        try {
            super.show();
        } catch (Exception e) {

        }
    }

}
